package com.k.comnote.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.k.comnote.R;
import com.k.comnote.database.NoteDB;
import com.k.comnote.model.Note;

/**
 * Created by K on 2015/8/19.
 */
public class NoteKeep extends ActionBarActivity {

    private Toolbar toolbar;
    private EditText noteContent;
    private EditText noteTitle;
    private NoteDB db;
    private TextView tvTime;
    private String currentTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_writenote);

        initView();
        initToolBar();
        currentTime = getTime();
        setTime(currentTime);

    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.writenote_toolbar);
        noteContent = (EditText) findViewById(R.id.edit_note);
        noteTitle = (EditText) findViewById(R.id.edit_title);
        tvTime = (TextView) findViewById(R.id.tv_time);
        db = NoteDB.getNoteDB(this);
    }

    private void initToolBar() {
        toolbar.setTitle(" ");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); //设置是否显示返回键的图标
        getSupportActionBar().setHomeButtonEnabled(true); //将应用程序图标设置为可点击的按钮
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ac_bar_save_note);
        getSupportActionBar().setDisplayShowHomeEnabled(false);//
    }

    private String getTime() {
        String time;
        Time t = new Time();
        t.setToNow();
        /**
         * 时间表示格式化
         */
        int year = t.year;
        int month = t.month;
        if(month<10-1) {
            time = year + " - " +"0"+ (month + 1) +" - ";
        } else time=year +" - "+(month+1)+" - ";
        int day = t.monthDay;
        if(day<10) {
            time += "0" + day + "    ";
        } else time  += day+"    ";
        int hour = t.hour;
        if(hour<10) {
            time += "0"+hour+" : ";
        }else time += hour  + " : ";
        int minute = t.minute;
        if(minute<10) {
            time += "0"+minute;
        }else time += minute;

        return time;
    }

    private void setTime(String currentTime) {
        tvTime.setText(currentTime);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_writenote, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                saveNote();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void saveNote() {
        String content = noteContent.getText().toString();
        String title = noteTitle.getText().toString();
        if (content.isEmpty()) {
            Toast.makeText(getApplicationContext(), "不能保存一条空日记", Toast.LENGTH_SHORT).show();
        } else {
            if (title.isEmpty()&& content.length()>=10) {
                title = content.substring(0, 10) + "...";
            } else if (title.isEmpty()){
                title = content;
            }
            Note note = new Note();
            note.setContent(content);
            note.setTitle(title);
            note.setTime(currentTime);
            db.saveNote(note);
            
            Toast.makeText(getApplicationContext(), "日记保存成功" + db.loadNote().size(), Toast.LENGTH_SHORT).show();
        }
        finish();
    }

}
